<?php 

class FRM_Tools_Uri
{
	/**
	 * the clean request uri
	 *
	 * @var string
	 */
	protected $_uri;
	
	/**
	 * load the uri
	 *
	 */
	public function __construct()
	{
		$uri = $_SERVER['REQUEST_URI'];
		$this->_uri = $this->cleanUri($uri);
	}
	
	/**
	 * if uri is not set then it will return the uri that was set in the constructor 
	 *
	 * @param string $uri
	 * @return array
	 */
	public function toArray($uri = false)
	{
		if($uri)
		{
			$uri = $this->cleanUri($uri);
		}else{
			$uri = $this->_uri;
		}
		$arr = explode('/', $uri);
		if(is_array($arr))
		{
			foreach ($arr as $part)
			{
				if(!empty($part))
				{
					$return[] = (string)$part;
				}
			}
			if(isset($return))
			{
				return $return;
			}
		}
	}
	/**
	 * Enter description here...
	 *
	 * @param string $val
	 * @param string $from
	 * @return string
	 */
	public function toString($val='', $from='')
	{
		$link = $this->_uri;
		
		if(!$val)
			$val = '';
		
		if($val || $from){
			if(FRM_Tools_String::getSelfFromPath($link)===$from){
    			$link = FRM_Tools_String::getParentFromPath($link).'/'.$val;
    		}else{
    			$link = $link.'/'.$val;
    		}
		}
		return $link;
	}
	
	/**
	 * cleans the uri
	 *
	 * @param string $uri
	 * @return string
	 */
	private function cleanUri($uri)
	{
		$uri = FRM_Tools_Regex::stripFileExtension($uri); 
		$uri = FRM_Tools_Regex::stripTrailingSlash($uri);
		return FRM_Tools_String::stripHyphens($uri);
	}
}